//
//  MNDBackMoneyHeaderView.m
//  minidai
//
//  Created by Steven.Yung on 2017/9/29.
//  Copyright © 2017年 迷你金服. All rights reserved.
//

#import "MNDBackMoneyHeaderView.h"
#import "NSDate+JKExtension.h"
#import "UIImageView+JKAddition.h"

@interface MNDBackMoneyHeaderView ()

@property (nonatomic, strong) UIButton *expandButton;
@property (nonatomic, strong) UIImageView *expandArrow;
@property (nonatomic, strong) UIView *whiteView;
@property(nonatomic, strong) UIView *gapView;
@property(nonatomic, strong) UIColor *bottomColor;
@property(nonatomic, strong) UIView *leftBottomView;
@property(nonatomic, strong) UIView *rightBottomView;

@end

@implementation MNDBackMoneyHeaderView

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithReuseIdentifier: reuseIdentifier];
    
    if (self) {
        self.contentView.backgroundColor = kColorF5;
        
        self.bottomColor = [UIColor colorWithRed:252/255.0 green:252/255.0 blue:252/255.0 alpha:1.0];
        
        [self makeConstraints];
    }
    
    return self;
}

- (void)setModel:(MNDReturnListSectionModel *)model {
    _model = model;
    
    NSDate *date = [NSDate jk_dateWithString:model.dayString format:@"yyyy-MM-dd"];
    self.dateLabel.text = [NSString stringWithFormat:@"%02zd", date.jk_day];
    self.yearLabel.text = [NSString stringWithFormat:@"%zd年", date.jk_year];
    self.monthLabel.text = [NSString stringWithFormat:@"%02zd月", date.jk_month];
    self.returnMoneyLabel.text = _model.returnMoney;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        if (self.model.isExpand) {
            
            self.leftBottomView.backgroundColor = self.bottomColor;
            self.rightBottomView.backgroundColor = self.bottomColor;
            self.expandArrow.transform = CGAffineTransformIdentity;
            
        }else{
            self.leftBottomView.backgroundColor = [UIColor clearColor];
            self.rightBottomView.backgroundColor = [UIColor clearColor];
            self.expandArrow.transform = CGAffineTransformMakeRotation(M_PI);
        }
    }];
}

- (void)expandAction:(UIButton *)sender {
    self.model.isExpand = !self.model.isExpand;
    
    [UIView animateWithDuration:0.3 animations:^{
        
        if (self.model.isExpand) {
            self.expandArrow.transform = CGAffineTransformIdentity;
            
        }else{
            self.expandArrow.transform = CGAffineTransformMakeRotation(M_PI);
        }
    }];
    
    if (self.headerClickedBlock) {
        self.headerClickedBlock(self.model.isExpand);
    }
}

- (void)makeConstraints {

    CGFloat margin = 15.0;
    
    [self.whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).offset(margin);
        make.trailing.equalTo(self.contentView).offset(-margin);
        make.top.equalTo(self.contentView).offset(10);
        make.bottom.equalTo(self.contentView).offset(-10);
    }];
    
    self.whiteView.layer.shadowRadius = 5;
    self.whiteView.layer.shadowOffset = CGSizeMake(-3, 0);
    self.whiteView.layer.shadowColor = kColorShadow.CGColor;
    self.whiteView.layer.shadowOpacity = 0.4;
    
    UIView *shadowView = [[UIView alloc] initWithFrame:CGRectZero];
    shadowView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:shadowView];
    
    [shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.whiteView);
    }];
    
    shadowView.layer.shadowRadius = 5;
    shadowView.layer.shadowOffset = CGSizeMake(3, 0);
    shadowView.layer.shadowColor = kColorShadow.CGColor;
    shadowView.layer.shadowOpacity = 0.4;
    
    [self.contentView sendSubviewToBack:shadowView];
    
    UIView *bottomLeftShadowView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.contentView addSubview:bottomLeftShadowView];
    [self.contentView sendSubviewToBack:bottomLeftShadowView];
//    bottomLeftShadowView.backgroundColor = self.bottomColor;
    
    [bottomLeftShadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).offset(margin);
        make.trailing.equalTo(self.contentView).offset(-margin);
        make.bottom.equalTo(self.contentView).offset(2);
        make.height.mas_equalTo(15.0);
    }];
    
    bottomLeftShadowView.layer.shadowRadius = 0;
    bottomLeftShadowView.layer.shadowOffset = CGSizeMake(2, 0);
    bottomLeftShadowView.layer.shadowColor = kColorLightGray.CGColor;
    bottomLeftShadowView.layer.shadowOpacity = 0.1;
    
    self.leftBottomView = bottomLeftShadowView;
    
    UIView *bottomRightShadowView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.contentView addSubview:bottomRightShadowView];
    [self.contentView sendSubviewToBack:bottomRightShadowView];
//    bottomRightShadowView.backgroundColor = self.bottomColor;
    
    [bottomRightShadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.contentView).offset(margin);
        make.trailing.equalTo(self.contentView).offset(-margin);
        make.bottom.equalTo(self.contentView).offset(2);
        make.height.mas_equalTo(15.0);
    }];
    
    bottomRightShadowView.layer.shadowRadius = 0;
    bottomRightShadowView.layer.shadowOffset = CGSizeMake(-2, 0);
    bottomRightShadowView.layer.shadowColor = kColorLightGray.CGColor;
    bottomRightShadowView.layer.shadowOpacity = 0.1;
    
    self.rightBottomView = bottomRightShadowView;
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.whiteView).offset(margin);
        make.centerY.equalTo(self.whiteView);
    }];
    
    [self.gapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.dateLabel.mas_trailing).offset(8.0);
        make.centerY.equalTo(self.dateLabel);
        make.width.mas_equalTo(2);
        make.height.mas_equalTo(30);
    }];
    
    [self.yearLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.gapView).offset(8.0);
        make.top.equalTo(self.gapView);
    }];
    
    [self.monthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.yearLabel);
        make.bottom.equalTo(self.gapView);
    }];
    
    [self.expandArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.equalTo(self.whiteView).offset(-margin);
        make.centerY.equalTo(self.whiteView);
    }];
    
    [self.returnMoneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.whiteView);
        make.trailing.equalTo(self.expandArrow.mas_leading).offset(-8);
    }];
    
    [self.expandButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.whiteView);
    }];
}

#pragma mark - Getter

- (UIView *)whiteView {
    if (!_whiteView) {
        _whiteView = [[UIView alloc] init];
        _whiteView.backgroundColor = [UIColor whiteColor];
        
        [self.contentView addSubview:_whiteView];
    }
    
    return _whiteView;
}

- (UIView *)gapView {
    if (!_gapView) {
        _gapView = [[UIView alloc] initWithFrame:CGRectZero];
        _gapView.backgroundColor = kColorLightGrayLine;
        
        [self.whiteView addSubview:_gapView];
    }
    
    return _gapView;
}

- (UILabel *)dateLabel {
    if (!_dateLabel) {
        _dateLabel = [self labelWithDefaultText:@"09" textColor:kColorBlack fontSize:29];
        
        [self.whiteView addSubview:_dateLabel];
    }
    
    return _dateLabel;
}

- (UILabel *)yearLabel {
    if (!_yearLabel) {
        _yearLabel = [self labelWithDefaultText:@"2017年" textColor:kColorAF fontSize:11];
        
        [self.whiteView addSubview:_yearLabel];
    }
    
    return _yearLabel;
}

- (UILabel *)monthLabel {
    if (!_monthLabel) {
        _monthLabel = [self labelWithDefaultText:@"09月" textColor:kColorAF fontSize:11];
        
        [self.whiteView addSubview:_monthLabel];
    }
    
    return _monthLabel;
}

- (UILabel *)returnMoneyLabel {
    if (!_returnMoneyLabel) {
        _returnMoneyLabel = [self labelWithDefaultText:@"0.00" textColor:kColorBlack fontSize:21.0];
        
        [self.whiteView addSubview: _returnMoneyLabel];
    }
    
    return _returnMoneyLabel;
}

- (UIButton *)expandButton {
    if (!_expandButton) {
        _expandButton = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [_expandButton addTarget:self action:@selector(expandAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.whiteView addSubview:_expandButton];
        
    }
    
    return _expandButton;
}

- (UIImageView *)expandArrow {
    if (!_expandArrow) {
        _expandArrow = [UIImageView jk_imageViewWithImageNamed:@"investment_detail_expand"];
        
        [self.whiteView addSubview: _expandArrow];
    }
    
    return _expandArrow;
}

- (UILabel *)labelWithDefaultText:(NSString *)text textColor:(UIColor *)color fontSize:(CGFloat)size {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.text = text;
    label.textColor = color;
    label.font = [UIFont fontWithName:@"PingFangSC-Regular" size:size];
    
    return label;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
